package com.whfc.ms.api.sys.controller;

import com.whfc.common.result.PageData;
import com.whfc.common.result.Result;
import com.whfc.common.result.ResultUtil;
import com.whfc.common.util.SessionAttr;
import com.whfc.env.dto.GasDetectionQueryReq;
import com.whfc.env.dto.GasDetectionResultDTO;
import com.whfc.emp.dto.AppFenceDTO;
import com.whfc.entity.dto.Apply.rep.ArrivalConfirmationDTO;
import com.whfc.entity.dto.Apply.rep.RegionRep;
import com.whfc.entity.dto.Apply.rep.WorkPlan;
import com.whfc.entity.dto.Apply.req.*;
import com.whfc.fuum.entity.SysUser;
import com.whfc.fuum.entity.WxUser;
import com.whfc.mach.dto.OpenApiMachDTO;
import com.whfc.ms.api.sys.dto.ConfirmRequestDTO;
import com.whfc.ms.api.sys.dto.WorkPlanDTO;
import com.whfc.ms.api.sys.service.SysWorkPlanService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Objects;

/**
 * @author xjy
 * @description 谢江泳
 * @date 2025-09-13
 *       工作计划
 */
@RestController
@RequestMapping("/mp/api/workPlan")
public class SysWorkPlanController {

    @Autowired
    private SysWorkPlanService workPlanService;


     /**
     * 获取审核计划列表
     * @return
     */
     @GetMapping("/work/list")
     public Result queryReviewList(@SessionAttribute(SessionAttr.MS_USER) SysUser user,Integer deptId) {
         List<WorkPlan> institutions = workPlanService.entryReview(user,deptId,null,null);
         return ResultUtil.success(institutions);
     }

    /**
     * 获取审核计划列表分页
     * 
     * @return
     */
    @GetMapping("/work/pageList")
    public Result queryReviewPageList(@SessionAttribute(SessionAttr.MS_USER) SysUser user,
            AppEntryApplicationReq request) {
        PageData<WorkPlanDTO> workPlanPage = workPlanService.queryReviewpageList(user, request);

        return ResultUtil.success(workPlanPage);
    }

    /**
     * 获取审核计划列表对外接口
     *
     * @return
     */
    @GetMapping("/work/reviewpageList")
    public Result reviewpageList(SysUser user,
                                      AppEntryApplicationReq request) {
        List<WorkPlanDTO> workPlanPage = workPlanService.reviewpageList(user, request);

        return ResultUtil.success(workPlanPage);
    }

    @GetMapping(value = "/areaList")
    public Result areaList() {
        List<AppFenceDTO> list = workPlanService.areaList();
        return ResultUtil.success(list);
    }

    /**
     * 添加工作计划
     *
     * @return 描述申请操作响应的字符串
     */
    @PostMapping("/saveWorkPlan")
    public Result saveWorkPlan(@SessionAttribute(SessionAttr.MS_USER) SysUser user,
            @RequestBody WorkPlanCreatePlanReq req) {
        String msg = workPlanService.saveWorkPlan(user, req);
        if (!Objects.equals(msg, "提交成功")) {
            return ResultUtil.failure();
        }
        return ResultUtil.success();
    }

    /**
     * 修改工作计划
     *
     * @param user
     * @param req
     * @return
     */
    @PostMapping("/edit")
    public Result edit(@SessionAttribute(SessionAttr.MS_USER) SysUser user, @RequestBody WorkPlanCreatePlanReq req) {
        workPlanService.edit(user, req);
        return ResultUtil.success();
    }

    /**
     * 工作计划详情
     * 
     * @param id 工作计划id
     * @return
     */
    @GetMapping("/detail")
    public Result detail(Integer id) {
        WorkPlan application = workPlanService.detail(id);
        return ResultUtil.success(application);
    }

    /**
     * 获取入场Id
     * 
     * @return
     */
    @GetMapping("/queryEntryerId")
    public Result queryEntryerId(@SessionAttribute(SessionAttr.MS_USER) SysUser user) {
        if (user.getId() == null) {
            return ResultUtil.failure("当前用户未登录,请重新登录!");
        }
        return ResultUtil.success(workPlanService.selectByCreator(user));
    }

    /**
     * 获取施工人員
     * 
     * @param id 入场凭证id
     * @return
     */
    @GetMapping("/queryWorkers")
    public Result queryWorkers(Integer id) {
        if (id == null) {
            return ResultUtil.failure("请选择入场凭证!");
        }
        List<SafetyTrainingExamReq> application = workPlanService.queryConstructionWorkers(id);
        return ResultUtil.success(application);
    }

    /**
     * 获取工作区域
     * 
     * @param deptId 项目Id
     * @return
     */
    @GetMapping("/queryWorkArea")
    public Result queryWorkArea(@RequestParam("deptId") Integer deptId) {
        if (deptId == null) {
            return ResultUtil.failure("项目id不能为空");
        }
        List<RegionRep> application = workPlanService.queryWorkArea(deptId);
        return ResultUtil.success(application);
    }

    /**
     * 获取设备清单
     * 
     * @param deptId 项目Id
     * @return
     */
    @GetMapping("/queryMach")
    public Result queryMach(@RequestParam("deptId") Integer deptId) {
        if (deptId == null) {
            return ResultUtil.failure("项目id不能为空");
        }
        List<OpenApiMachDTO> application = workPlanService.queryMach(deptId);
        return ResultUtil.success(application);
    }

    /**
     * 获取到场||离场列表支持分页
     * 
     * @return
     */
    @GetMapping("/list")
    public Result list(AppEntryApplicationReq req) {
        PageData<ArrivalConfirmationDTO> workDailyReportrep = workPlanService.list(req);
        return ResultUtil.success(workDailyReportrep);
    }

    /**
     * 获取到场确认
     * 
     * @return
     */
    @PostMapping("/submit")
    public Result submitConfirm(@SessionAttribute(SessionAttr.MS_USER) SysUser user,
            @RequestBody ConfirmRequestDTO requestDTO) {
        int rows = workPlanService.confirm(user, requestDTO);
        return rows > 0 ? ResultUtil.success("确认成功") : ResultUtil.failure("确认失败");
    }

    /**
     * 获取到场确认列表
     * @return
     */
    @PostMapping("/submit/list")
    public Result submitConfirmList(@SessionAttribute(SessionAttr.MS_USER) SysUser user, @RequestBody ConfirmRequestDTO requestDTO) {
        return ResultUtil.success(workPlanService.confirmList(user,requestDTO));
    }

    /**
     * 审核
     *
     * @param id
     * @return
     */
    @GetMapping("/review")
    public Result review(@SessionAttribute(SessionAttr.MS_USER) SysUser user,
            @RequestParam("approvalResult") Integer approvalResult, @RequestParam("id") Integer id) {
        String review = workPlanService.review(user, approvalResult, id);
        if (Objects.equals(review, "审核成功!")) {
            return ResultUtil.success(review);
        } else {
            return ResultUtil.failure(review);
        }
    }

    /**
     * 导出工作计划审核列表
     * 
     * @param user 当前用户
     * @param request 查询请求参数
     * @return 导出文件下载地址
     */
    @PostMapping("/work/export")
    public Result exportWorkPlanReviewList(@SessionAttribute(SessionAttr.MS_USER) SysUser user,
                                          @RequestBody AppEntryApplicationReq request) {
        String fileUrl = workPlanService.exportWorkPlanReviewList(user, request);
        return ResultUtil.success(fileUrl);
    }

    /**
     * 获取最新气体信息
     * @param queryReq
     * @return
     */
    @PostMapping("/gasDetection")
    public Result getGasDetectionData(@RequestBody GasDetectionQueryReq queryReq) {
        GasDetectionResultDTO result = workPlanService.getGasDetectionData(queryReq);
        return ResultUtil.success(result);
    }
}